package com.alander.nexusgo.framework.file.starter.config;

import com.alander.nexusgo.framework.file.starter.model.OssProperties;
import com.alander.nexusgo.framework.file.starter.service.FileUploadService;
import com.alander.nexusgo.framework.file.starter.service.impl.OssFileUploadService;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(OssProperties.class)
public class OssAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public OSS ossClient(OssProperties ossProperties) {
        return new OSSClientBuilder().build(
                ossProperties.getEndpoint(),
                ossProperties.getAccessKeyId(),
                ossProperties.getAccessKeySecret()
        );
    }

    @Bean
    @ConditionalOnMissingBean
    public FileUploadService fileUploadService(OSS ossClient, OssProperties ossProperties) {
        return new OssFileUploadService(ossClient, ossProperties);
    }
}

